package com.motu.monstercity.server.game.logic;

import com.motu.monstercity.server.game.commondata.build.BuildMain;
import com.motu.monstercity.server.game.userdata.*;
import com.motu.vertx.module.utility.model.EntityManager;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonArray;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.jar.JarEntry;

public class BuildUserData extends BuildBaseData {
    public static UserCityInfo getUserCityInfo(long userId) {
        return (UserCityInfo) EntityManager.getUniqueEntity(UserCityInfo.class, userId);
    }

    public static UserSoldierRecover getUserSoldierRecover(long userId) {
        return (UserSoldierRecover) EntityManager.getUniqueEntity(UserSoldierRecover.class, userId);
    }

    public static UserBuild getUserBuild(long userId, int buildId) {
        return (UserBuild) EntityManager.getEntity(UserBuild.class, userId, buildId);
    }

    // 获取userbuild列表
    public static List<UserBuild> getUserBuildList(long userId) {
        List<UserBuild> list = new ArrayList<>();
        for (Model model : EntityManager.getEntityList(UserBuild.class, userId)) {
            UserBuild entity = (UserBuild) model;
            list.add(entity);
        }
        return list;
    }

    // 获取某个类型的所有建筑列表
    public static List<UserBuild> getUserBuildListByType(long userId, int type) {
        JsonArray jsonArray = new JsonArray();
        for (BuildMain buildMain : getBuildMainMap().values())  {
            if (buildMain.getType() == type) {
                jsonArray.add(buildMain.getBuildId());
            }
        }

        List<UserBuild> list = new ArrayList<>();
        for (Model model : EntityManager.getEntityList(UserBuild.class, userId, jsonArray)) {
            UserBuild entity = (UserBuild) model;
            list.add(entity);
        }
        return list;
    }

    public static UserBuildTile getUserBuildTile(long userId, int tileId) {
        return (UserBuildTile) EntityManager.getEntity(UserBuildTile.class, userId, tileId);
    }

    public static List<UserBuildTile> getUserBuildTileList(long userId) {
        List<UserBuildTile> list = new ArrayList<>();
        for (Model model : EntityManager.getEntityList(UserBuildTile.class, userId)) {
            UserBuildTile entity = (UserBuildTile) model;
            list.add(entity);
        }
        return list;
    }

    public static UserCityCoord getUserCityCoord(long userId, long coord) {
        return (UserCityCoord) EntityManager.getEntity(UserCityCoord.class, userId, coord);
    }

    // 获取UserCityCoord列表
    public static List<UserCityCoord> getUserCityCoordList(long userId) {
        List<UserCityCoord> list = new ArrayList<>();
        for (Model model : EntityManager.getEntityList(UserCityCoord.class, userId)) {
            UserCityCoord entity = (UserCityCoord) model;
            list.add(entity);
        }
        return list;
    }

    public static ConcurrentHashMap<Long, UserCityCoord> getUserCityCoordList(long userId, JsonArray coordList) {
        ConcurrentHashMap<Long, UserCityCoord> result = new ConcurrentHashMap<>();
        for (Model model : EntityManager.getEntityList(UserCityCoord.class, userId, coordList)) {
            UserCityCoord entity = (UserCityCoord) model;
            result.put(entity.getCrood(), entity);
        }
        return result;
    }

    // 获取userpartner列表
    public static ConcurrentHashMap<Integer, UserPartner> getUserPartnerList(long userId, JsonArray partnerIdList) {
        ConcurrentHashMap<Integer, UserPartner> result = new ConcurrentHashMap<>();
        for (Model model : EntityManager.getEntityList(UserPartner.class, userId, partnerIdList)) {
            UserPartner entity = (UserPartner) model;
            result.put(entity.getPartnerId(), entity);
        }
        return result;
    }
}
